package controller

import (
	"model"
	"mvc"
	"fmt"
)

type Home struct{}

func (this *Home) Init() {
	//initilize any variable of Home
}

func (this *Home) Index() *mvc.ActionResult {
	v := model.ListArticle()
	return mvc.View(v)
}


func (this *Home) LogOn() *mvc.ActionResult {
	return mvc.View(nil)
}

func (this *Home) LogOnPost(model *model.LogOnModel) *mvc.ActionResult {

	if model.UserName == "cj" && model.Password == "123" {

		return mvc.RedirectToAction("home", "index")
	}

	return mvc.Render(`Username/Password incorrect!`)
}

func (this *Home) View(title string) *mvc.ActionResult {
	v := model.ListArticle()
	a := v.GetArticle(title)
	return mvc.View(a)
}

func (this *Home) Add() *mvc.ActionResult {
	return mvc.View(nil)
}

func (this *Home) AddPost(model *model.Article) *mvc.ActionResult {
	err := model.Save()
	if err == nil {
		fmt.Println("No error")
		return mvc.RedirectToAction("home", "view", model.Title)
	}

	return mvc.Error(err.String())
}
